﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BenhVien
{
    public partial class BenhNhanNT : Form
    {
        private SqlDataAdapter da = new SqlDataAdapter();
        private DataTable dt = new DataTable();
        private Connect conn = new Connect();
        public BenhNhanNT()
        {
            InitializeComponent();
        }

        private void BenhNhanNT_Load(object sender, EventArgs e)
        {
            conn.connect();
            getdata();
            binding();
        }
        public void getdata() 
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select * from BenhNhanNoiTru";
            da.SelectCommand = command;
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
        private void binding()
        {
            txtMP.DataBindings.Clear();
            txtMP.DataBindings.Add("Text", dataGridView1.DataSource, "MaPhong");
            txtMNV.DataBindings.Clear();
            txtMNV.DataBindings.Add("Text", dataGridView1.DataSource, "MaNV");
            txtMBN.DataBindings.Clear();
            txtMBN.DataBindings.Add("Text", dataGridView1.DataSource, "MaBN");
            txtMG.DataBindings.Clear();
            txtMG.DataBindings.Add("Text", dataGridView1.DataSource, "MaGiuong");
            txtDSMR.DataBindings.Clear();
            txtDSMR.DataBindings.Add("Text", dataGridView1.DataSource, "DauSoMoRong");
            d1.DataBindings.Clear();
            d1.DataBindings.Add("Text", dataGridView1.DataSource, "NgayVaoDanhSachCho");
            txtSNODK.DataBindings.Clear();
            txtSNODK.DataBindings.Add("Text", dataGridView1.DataSource, "SoNgayODuKien");
            d2.DataBindings.Clear();
            d2.DataBindings.Add("Text", dataGridView1.DataSource, "NgayNhapVien");
            d3.DataBindings.Clear();
            d3.DataBindings.Add("Text", dataGridView1.DataSource, "NgayRaVien");
            d4.DataBindings.Clear();
            d4.DataBindings.Add("Text", dataGridView1.DataSource, "NgayRaThucSu");
        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            if (txtMP.Text == "" | txtMBN.Text == "" | txtMNV.Text == "" | txtDSMR.Text == "" | txtMG.Text == "" | txtSNODK.Text == "" | d1.Text == "" | d2.Text == "" | d3.Text == "" | d4.Text == "")
                MessageBox.Show("Chua Nhap du thong tin", "Loi Nhap", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                if (check1("Select MaPhong from LichLamViec where MaPhong = '" + txtMP.Text + "'") == true)
                {
                    if (check1("Select MaNV from NhanVien where MaNV = '" + txtMNV.Text + "'") == true)
                    {
                        if (check1("Select MaBN from BenhNhanNoiTru where MaBN = '" + txtMBN.Text + "'") == false)
                        {
                            if (check1("Select MaBN from BenhNhan where MaBN = '" + txtMBN.Text + "'") == true)
                            {
                                if (check1("Select MaGiuong from Giuong where MaGiuong = '" + txtMG.Text + "'") == true)
                                {
                                    if (check1("Select TinhTrang from Giuong where TinhTrang = 1  and MaGiuong = '" + txtMG.Text + "'") == true)
                                    {

                                        DataRow row = dt.NewRow();
                                        row["MaPhong"] = txtMP.Text;
                                        row["MaNV"] = txtMNV.Text;
                                        row["DauSoMoRong"] = txtDSMR.Text;
                                        row["MaBN"] = txtMBN.Text;
                                        row["NgayVaoDanhSachCho"] = d1.Value;
                                        row["SoNgayODuKien"] = txtSNODK.Text;
                                        row["NgayNhapVien"] = d2.Value;
                                        row["NgayRaVien"] = d3.Value;
                                        row["NgayRaThucSu"] = d4.Value;
                                        row["MaGiuong"] = txtMG.Text;
                                        dt.Rows.Add(row);
                                        SqlCommand commandinsert = new SqlCommand();
                                        commandinsert.Connection = conn.con;
                                        commandinsert.CommandType = CommandType.Text;
                                        commandinsert.CommandText = @"Insert BenhNhanNoiTru (MaPhong, MaNV, DauSoMoRong, MaBN, NgayVaoDanhSachCho, SoNgayODuKien, NgayNhapVien, NgayRaVien, NgayRaThucSu, MaGiuong) values
                                                                            (@MaPhong, @MaNV, @DauSoMoRong, @MaBN, @NgayVaoDanhSachCho, @SoNgayODuKien, @NgayNhapVien, @NgayRaVien, @NgayRaThucSu, @MaGiuong)";
                                        commandinsert.Parameters.Add("@MaPhong", SqlDbType.NVarChar, 50).Value = txtMP.Text;
                                        commandinsert.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50).Value = txtMNV.Text;
                                        commandinsert.Parameters.Add("@MaBN", SqlDbType.NVarChar, 50).Value = txtMBN.Text;
                                        commandinsert.Parameters.Add("@MaGiuong", SqlDbType.NVarChar, 50).Value = txtMG.Text;
                                        commandinsert.Parameters.Add("@DauSoMoRong", SqlDbType.NVarChar, 50).Value = txtDSMR.Text;
                                        commandinsert.Parameters.Add("@NgayVaoDanhSachCho", SqlDbType.Date, 50).Value = d1.Value;
                                        commandinsert.Parameters.Add("@SoNgayODuKien", SqlDbType.Int, 10).Value = txtSNODK.Text;
                                        commandinsert.Parameters.Add("@NgayNhapVien", SqlDbType.Date, 50).Value = d2.Value;
                                        commandinsert.Parameters.Add("@NgayRaVien", SqlDbType.Date, 50).Value = d3.Value;
                                        commandinsert.Parameters.Add("@NgayRaThucSu", SqlDbType.Date, 50).Value =d4.Value;
                                        da.InsertCommand = commandinsert;
                                        da.Update(dt);
                                        MessageBox.Show("Them Thanh Cong", "Chuc Mung", MessageBoxButtons.OK);

                                    }
                                    else MessageBox.Show("Giuong Da Co Benh Nhan", "Loi", MessageBoxButtons.OK);
                                }
                                else MessageBox.Show("Ma Giuong Khong Dung", "Loi", MessageBoxButtons.OK);
                            }
                            else MessageBox.Show("Ma Benh Nhan Sai", "Loi", MessageBoxButtons.OK);
                        }
                        else MessageBox.Show("Benh Nhan Da Ton Tai", "Loi", MessageBoxButtons.OK);
                    }
                    else MessageBox.Show("Ma Nhan Vien Bi Sai", "Loi", MessageBoxButtons.OK);
                }
                else MessageBox.Show("Ma Phong Khong Ton Tai", "Loi", MessageBoxButtons.OK);
            }
            updateGiuong();
        }
        public void updateGiuong()
        {
                DataTable dt1 = new DataTable();
                SqlCommand command = new SqlCommand();
                command.Connection = conn.con;
                command.CommandType = CommandType.Text;
                command.CommandText = @"select * from Giuong";
                da.SelectCommand = command;
                da.Fill(dt1);
                DataRow row = dt1.Select("MaGiuong like '" + txtMG.Text + "'")[0];
                row.BeginEdit();
                row["MaPhong"] = txtMP.Text;
                row["MaGiuong"] = txtMG.Text;
                row["TinhTrang"] = 0;
                row.EndEdit();
                SqlCommand commandUpdate = new SqlCommand();
                commandUpdate.Connection = conn.con;
                commandUpdate.CommandType = CommandType.Text;
                commandUpdate.CommandText = @"Update Giuong Set 
                                                              MaPhong=@MaPhong,
                                                              TinhTrang= @TinhTrang
                                                              where MaGiuong=@MaGiuong";
                commandUpdate.Parameters.Add("@MaPhong", SqlDbType.NVarChar, 50, "MaPhong");
                commandUpdate.Parameters.Add("@MaGiuong", SqlDbType.NVarChar, 50, "MaGiuong");
                commandUpdate.Parameters.Add("@TinhTrang", SqlDbType.Int, 10, "TinhTrang");
                da.UpdateCommand = commandUpdate;
                da.Update(dt1);
        }
        private bool check(String s, String s1)
        {
            bool check = false;
            DataTable data = new DataTable();
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = s;
            command.Parameters.Add("@DoiSo", SqlDbType.NVarChar, 50).Value = s1;
            da.SelectCommand = command;
            da.Fill(data);
            if (data.Rows.Count > 0) check = true;
            return check;
        }
        private bool check1(String s)
        {
            bool check1 = false;
            DataTable data = new DataTable();
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = s;
            da.SelectCommand = command;
            da.Fill(data);
            if (data.Rows.Count > 0) check1 = true;
            return check1;
        }

        private void bt_NhapMoi_Click(object sender, EventArgs e)
        {
            txtDSMR.Text = ""; txtMBN.Text = ""; txtMG.Text = ""; txtMNV.Text = ""; txtMP.Text = "";
            txtSNODK.Text = ""; d1.Text = ""; d2.Text = ""; d3.Text = ""; d4.Text = "";
            bt_Luu.Enabled = false;
            bt_Xoa.Enabled = false;
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("MaBN like '" + txtMBN.Text + "'")[0];
            row.BeginEdit();
            row.Delete();
            row.EndEdit();
            SqlCommand commandDelete = new SqlCommand();
            commandDelete.Connection = conn.con;
            commandDelete.CommandType = CommandType.Text;
            commandDelete.CommandText = @"Delete from BenhNhanNoiTru Where MaBN=@MaBN";
            commandDelete.Parameters.Add("@MaBN", SqlDbType.NVarChar, 50, "MaBN");
            da.DeleteCommand = commandDelete;
            da.Update(dt);
            MessageBox.Show("ban da xóa thanh cong", "chuc mung", MessageBoxButtons.OK);
        }

        private void bt_Luu_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("MaBN like '" + txtMBN.Text + "'")[0];
            row.BeginEdit();
            row["MaPhong"] = txtMP.Text;
            row["MaNV"] = txtMNV.Text;
            row["MaBN"] = txtMBN.Text;
            row["MaGiuong"] = txtMG.Text;
            row["DauSoMoRong"] = txtDSMR.Text;
            row["NgayVaoDanhSachCho"] = d1.Value;
            row["SoNgayODuKien"] = txtSNODK.Text;
            row["NgayNhapVien"] = d2.Value;
            row["NgayRaVien"] = d3.Value;
            row["NgayRaThucSu"] = d4.Value;
            row.EndEdit();
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = conn.con;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"Update BenhNhanNoiTru Set 
                                                              DauSoMoRong= @DauSoMoRong,
                                                              NgayVaoDanhSachCho= @NgayVaoDanhSachCho,
                                                              SoNgayODuKien=@SoNgayODuKien,
                                                              NgayNhapVien=@NgayNhapVien,
                                                              NgayRaVien=@NgayRaVien,
                                                              NgayRaThucSu=@NgayRaThucSu
                                                              where MaBN= @MaBN and MaPhong=@MaPhong and MaNV=@MaNV and MaGiuong=@MaGiuong";
            commandUpdate.Parameters.Add("@MaPhong", SqlDbType.NVarChar, 50, "MaPhong");
            commandUpdate.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
            commandUpdate.Parameters.Add("@MaBN", SqlDbType.NVarChar, 50, "MaBN");
            commandUpdate.Parameters.Add("@MaGiuong", SqlDbType.NVarChar, 50, "MaGiuong");
            commandUpdate.Parameters.Add("@DauSoMoRong", SqlDbType.NVarChar, 50, "DauSoMoRong");
            commandUpdate.Parameters.Add("@NgayVaoDanhSachCho", SqlDbType.Date, 50, "NgayVaoDanhSachcho");
            commandUpdate.Parameters.Add("@SoNgayODuKien", SqlDbType.Int, 50, "SoNgayODuKien");
            commandUpdate.Parameters.Add("@NgayNhapVien", SqlDbType.Date, 50, "NgayNhapVien");
            commandUpdate.Parameters.Add("@NgayRaVien", SqlDbType.Date, 50, "NgayRaVien");
            commandUpdate.Parameters.Add("@NgayRaThucSu", SqlDbType.Date, 50, "NgayRaThucSu");
            da.UpdateCommand = commandUpdate;
            da.Update(dt);
            MessageBox.Show("ban da chinh sua thanh cong", "chuc mung", MessageBoxButtons.OK);
        }

        private void bt_Reset_Click(object sender, EventArgs e)
        {
            txtMBN.Enabled = false;
            txtMNV.Enabled = false;
            txtMG.Enabled = false;
            txtMP.Enabled = false;
            bt_Xoa.Enabled = true;
            bt_Luu.Enabled = true;
            dt.Clear();
            getdata();
        }
    }
}
